﻿using System;
using System.Linq;
using System.Runtime;
using AutoMapper;
using Enferno.Public.InversionOfControl;
using Enferno.Public.Web.Mappers.BasketProfiles;
using Enferno.Public.Web.Mappers.CheckoutProfiles;
using Enferno.Public.Web.Models;
using Enferno.Public.Web.Test.Mappers.ProductProfiles;
using Enferno.StormApiClient.Customers;
using Enferno.StormApiClient.Shopping;
using Enferno.Web.StormUtils;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;

namespace Enferno.Public.Web.Test.Mappers.CheckoutProfiles
{
    [TestClass]
    public class CheckoutProfileTests
    {
        [TestInitialize]
        public void Init()
        {
            IoC.RegisterType<ISiteRules, TestSiteRules>();

            Mapper.Configuration.AddProfile<BasketToBasketModelProfile>();
            Mapper.Configuration.AddProfile<BasketItemToBasketItemModelProfile>();

            Mapper.Configuration.AddProfile<CheckoutToCheckoutModelProfile>();

            Mapper.Configuration.AddProfile<AddressToAddressModelProfile>();
            Mapper.Configuration.AddProfile<CompanyToCompanyModelProfile>();
            Mapper.Configuration.AddProfile<CustomerToCustomerModelProfile>();
            Mapper.Configuration.AddProfile<DeliveryMethodToDeliveryMethodModelProfile>();
            Mapper.Configuration.AddProfile<PaymentMethodToPaymentMethodModelProfile>();
            Mapper.Configuration.AddProfile<PaymentServiceToPaymentserviceModelProfile>();
            Mapper.Configuration.AddProfile<PaymentResponseProfile>();

            var stormContext = MockRepository.GenerateMock<IStormContext>();
            stormContext.Stub(x => x.Configuration).Return(new StormConfigurationSection());
            stormContext.Stub(x => x.ShowPricesIncVat).Return(true);
            stormContext.Stub(x => x.CultureCode).Return("sv");
            StormContext.SetInstance(stormContext);
        }

        [TestMethod]
        public void MappingIsValid()
        {
            Mapper.AssertConfigurationIsValid();
        }

        [TestMethod]
        public void CheckoutToCompanyCheckoutModelTest()
        {
            var checkout = new Checkout()
            {
                Basket = new Basket
                {
                    DiscountCode = "blaj",
                    Items = new BasketItemList {new BasketItem {Price = 12, VatRate = 12}},
                    AppliedPromotions = new PromotionList()
                },
                Buyer = new Customer
                {
                    Email = "test@enferno.se",
                    FirstName = "Test",
                    LastName = "Tester",
                    DeliveryAddresses = new AddressList
                    {
                        new Address
                        {
                            City = "Stockholm",
                            Country = "Sweden",
                            Zip = "66666"
                        },
                        new Address
                        {
                            City = "Stockholm",
                            Country = "Estonia",
                            Zip = "11111"
                        }
                    }
                },
                Payer = new Customer
                {
                    Companies = new CompanyList
                    {
                        new Company
                        {
                            Id = 10,
                            Code = "code",
                            Name = "Enferno AB",
                            OrgNo = "999999-0000",
                            Phone = "+1 800 20 45 67",
                            InvoiceAddress = new Address
                            {
                                City = "Stockholm",
                                Country =  "Sweden",
                                Zip = "62230"
                            }
                        }
                    }
                },
                ShipTo = new Customer
                {
                    DeliveryAddresses = new AddressList
                    {
                        new Address
                        {
                            City = "Stockholm",
                            Country = "Sweden",
                            Zip = "66666"
                        },
                        new Address
                        {
                            City = "Stockholm",
                            Country = "Estonia",
                            Zip = "11111"
                        }
                    }
                }
            };

            var checkoutModel = Mapper.Map<Checkout, CompanyCheckoutModel>(checkout);

            Assert.IsNotNull(checkoutModel.Basket);

            Assert.IsNotNull(checkoutModel.Buyer);
            //Assert.AreEqual(1, checkoutModel.Buyer.Companies.Count());
            //Assert.AreEqual(2, checkoutModel.Buyer.DeliveryAddresses.Count());
            Assert.AreEqual(checkoutModel.Buyer.Email, checkout.Buyer.Email);
        }
    }
}
